package models

import (
	"github.com/jinzhu/gorm"
	"godemo/dao"
	"time"
)

func (Player) TableName() string {
	return "player"
}

type Player struct {
	Id         int       `json:"id"`
	ActivityId int       `json:"activity_id"`
	Name       string    `json:"name"`
	Desc       string    `json:"desc"`
	Avatar     string    `json:"avatar"`
	Score      int       `json:"score"`
	CreateTime time.Time `json:"create_time"`
	UpdateTime time.Time `json:"update_time"`
}

func FindPlayerList(activityId int) ([]Player, error) {
	var players []Player
	err := dao.Db.Debug().Where("activity_id=?", activityId).Order("score desc").Find(&players).Error
	return players, err
}

func UpdateScore(playerId int) error {
	player := Player{Id: playerId}
	err := dao.Db.Debug().Model(&player).UpdateColumn("score", gorm.Expr("score + ?", 1)).Error
	return err
}
